package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.query.ClassinfoQueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;

    @Autowired
    private IEmployeeService employeeService;

    //@RequiredPermission(name = "班级列表", expression = "classinfo:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ClassinfoQueryObject qo) {
        //分页查询
        PageInfo<Classinfo> pageInfo = classinfoService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询所有的班主任信息
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees", employees);
        return "classinfo/list";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo){
        //判断id是否存在
        if (classinfo.getId() != null) {
            //不为空为编辑操作
            classinfoService.update(classinfo);
        }else {
            //否则为新增操作
            classinfoService.save(classinfo);
        }
        return new JsonResult();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        //判断id是否存在
        if (id != null) {
            //不为空为编辑操作
            classinfoService.delete(id);
        }
        return new JsonResult();
    }

}

